package br.com.servlets;

import br.com.business.ClienteBO;
import br.com.business.FilmeBO;
import br.com.business.ItemBO;
import br.com.model.Cliente;
import br.com.model.Filme;
import br.com.model.Item;
import com.google.gson.Gson;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AjaxRequest extends HttpServlet {

    private static final long serialVersionUID = 1L;
    
    public AjaxRequest() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String param = req.getParameter("term");
        String action = req.getParameter("act");
        resp.setContentType("application/json");

        if(action.equals("autocomplete")){
            List<AutoCompleteData> result = new ArrayList<AutoCompleteData>();
            ClienteBO clienteBO = new ClienteBO();
            LinkedList<Cliente> clientes = clienteBO.listarCliente();

            for(Cliente cliente : clientes){
                if (cliente.getNome().toLowerCase().startsWith(param.toLowerCase())) {
                    result.add(new AutoCompleteData(cliente.getNome(), cliente.getNome()));
                }
            }
            resp.getWriter().write(new Gson().toJson(result));
        }
        
        if(action.equals("buscarfilme")){
            try{
                List<Item> result = new ArrayList<Item>();
                FilmeBO filmeBO = new FilmeBO();
                ItemBO itemBO = new ItemBO();
                LinkedList<Filme> filmes = filmeBO.getFilmeByName(param);
                
                for(Filme filme : filmes){
                    LinkedList<Item> itens = itemBO.getItemDoFilme(filme.getIdFilme());
                    for(Item item : itens){
                        //item.setEstado(EstadoItem.valueOf(param));
                        result.add(item);
                    }
                }
                resp.getWriter().write(new Gson().toJson(result));
            }
            catch(SQLException e){
                Logger.getLogger(AjaxRequest.class.getName()).log(Level.SEVERE, null, e);
            }
            catch(Exception e){
                Logger.getLogger(AjaxRequest.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        
        if(action.equals("buscaritem")){
            try{
                ItemBO itemBO = new ItemBO();
                Item result = itemBO.getObjectByNumeroSerie(param);
                resp.getWriter().write(new Gson().toJson(result));
            }
            catch(SQLException e){
                Logger.getLogger(AjaxRequest.class.getName()).log(Level.SEVERE, null, e);
            }
            catch(Exception e){
                Logger.getLogger(AjaxRequest.class.getName()).log(Level.SEVERE, null, e);
            }            
        }        
    }
}

class AutoCompleteData {
    private final String label;
    private final String value;

    public AutoCompleteData(String _label, String _value) {
        super();
        this.label = _label;
        this.value = _value;
    }

    public final String getLabel() {
        return this.label;
    }

    public final String getValue() {
        return this.value;
    }
}